// admin/system/service/sys_user_role_service.go
package service

import (
	repo "go-admin1/admin/system/dao"
	"go-admin1/admin/system/model"
)

// SysUserRoleService 用户角色关联服务接口
type SysUserRoleService interface {
	Create(userRole *model.SysUserRole) error
	GetByUserID(userID int64) ([]*model.SysUserRole, error)
	GetByRoleID(roleID int64) ([]*model.SysUserRole, error)
	DeleteByUserID(userID int64) error
	DeleteByRoleID(roleID int64) error
	DeleteByUserIDAndRoleID(userID, roleID int64) error
	ListRoleIDsByUserID(userID int64) ([]int64, error)
	ListUserIDsByRoleID(roleID int64) ([]int64, error)
}

type sysUserRoleService struct {
	userRoleDao repo.SysUserRoleDao
}

func (s sysUserRoleService) Create(userRole *model.SysUserRole) error {
	return s.userRoleDao.Create(userRole)
}

func (s sysUserRoleService) GetByUserID(userID int64) ([]*model.SysUserRole, error) {
	userRoles, err := s.userRoleDao.GetByUserID(userID)
	if err != nil {
		return nil, err
	}
	return userRoles, nil
}

func (s sysUserRoleService) GetByRoleID(roleID int64) ([]*model.SysUserRole, error) {
	userRoles, err := s.userRoleDao.GetByRoleID(roleID)
	if err != nil {
		return nil, err
	}
	return userRoles, nil
}

func (s sysUserRoleService) DeleteByUserID(userID int64) error {
	return s.userRoleDao.DeleteByUserID(userID)
}

func (s sysUserRoleService) DeleteByRoleID(roleID int64) error {
	return s.userRoleDao.DeleteByRoleID(roleID)
}

func (s sysUserRoleService) DeleteByUserIDAndRoleID(userID, roleID int64) error {
	return s.userRoleDao.DeleteByUserIDAndRoleID(userID, roleID)
}

func (s sysUserRoleService) ListRoleIDsByUserID(userID int64) ([]int64, error) {
	roleIDs, err := s.userRoleDao.ListRoleIDsByUserID(userID)
	if err != nil {
		return nil, err
	}
	return roleIDs, nil
}

func (s sysUserRoleService) ListUserIDsByRoleID(roleID int64) ([]int64, error) {
	userIDs, err := s.userRoleDao.ListUserIDsByRoleID(roleID)
	if err != nil {
		return nil, err
	}
	return userIDs, nil
}

// NewSysUserRoleService 创建用户角色关联服务实例
func NewSysUserRoleService() SysUserRoleService {
	return &sysUserRoleService{
		userRoleDao: repo.NewSysUserRoleDao(),
	}
}
